package cn.itcast.web.utils;


import cn.itcast.vo.BaiduMapResult;
import cn.itcast.vo.BaiduMapVo;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.cxf.jaxrs.client.WebClient;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddressUtils {

    private static ObjectMapper objectMapper = new ObjectMapper();

    public static Map<String, List<Float>> getCityAddress(List<String> city) throws IOException {

        Map<String, List<Float>> map = new HashMap<>();

        for (String s : city) {
            String resultJson = WebClient.create("http://api.map.baidu.com/geocoding/v3/?address=" + s + "&output=json&ak=McrVd6kWHPoXXs6wf0KjZHVAGeykq6cC").get(String.class);
            BaiduMapVo baiduMapVo = objectMapper.readValue(resultJson, BaiduMapVo.class);
            if(baiduMapVo.getStatus()==0){
                ArrayList<Float> list = new ArrayList<>();
                BaiduMapResult baiduMapResult = baiduMapVo.getResult();
                Map<String, Float> location = baiduMapResult.getLocation();
                Float lng = location.get("lng");
                Float lat = location.get("lat");
                list.add(lng);
                list.add(lat);
                map.put(s,list);
            }
        }
        return map;
    }
}
